package logic;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Vector;


public class ChunkRepository implements Serializable{

	/**
	 *  
	 */
	private static final long serialVersionUID = 1L;
	private Vector<Chunk> chunks;
	private long repositorySize, repositorySpaceOccupied;
	public ChunkRepository(long RepoSize)
	{
		chunks=new Vector<Chunk>();
		repositorySize=RepoSize;
		repositorySpaceOccupied=0;
	}
	public Vector<Chunk> getChunks() {
		return chunks;
	}
	public void setChunks(Vector<Chunk> chunks) {
		this.chunks = chunks;
	}
	public boolean addChunk(Chunk chunk)
	{
		return this.chunks.add(chunk);
	}
	public void saveInFile(String name) throws IOException
	{
		FileOutputStream fout = new FileOutputStream(name);
		@SuppressWarnings("resource")
		ObjectOutputStream oos = new ObjectOutputStream(fout);
		oos.writeObject(this);
	}
	public long getRepositorySize() {
		return repositorySize;
	}
	public void setRepositorySize(int repositorySize) {
		this.repositorySize = repositorySize;
	}
	public long getRepositorySpaceOccupied() {
		return repositorySpaceOccupied;
	}
	public void setRepositorySpaceOccupied(long repositorySpaceOccupied) {
		this.repositorySpaceOccupied = repositorySpaceOccupied;
	}
	public LinkedList<Chunk> getChunksToRemove()
	{
		LinkedList<Chunk> l = new LinkedList<Chunk>(getChunks());
		Collections.sort(l);
		return l;

	}
	public LinkedList<Chunk> getChunksToRemove(boolean auto)
	{
		LinkedList<Chunk> l;
		if(!auto)
			l = new LinkedList<Chunk>(getChunks());
		else {
			l = new LinkedList<Chunk>();
			for(int i =0; i< getChunks().size();i++)
			{
				if(getChunks().elementAt(i).getReplicationDeg()< getChunks().elementAt(i).getIps().size())
					l.add(getChunks().elementAt(i));
			}
		}
		return l;

	}

}
